<?php
namespace Core\CoreComponents\TasksManager;
use Core;

require_once(COMPONENTS_DIR . 'TasksManager/ITasks.php');
require_once(CONTRACTS_DIR . 'task.entity.php');

/**
* This interface provide all accessible methods on task module
*
* @method ServiceResponse getTasks($user)
* @method ServiceResponse addTask($name)
* @method ServiceResponse deleteTask($id)
* @method ServiceResponse updateTask($id, $status)
*/
class Tasks implements ITasks
{
	/**
	 * Add a task
	 *
	 * @param string $name The task name
	 * @param User $user The affected user
	 * 
	 * @return ServiceResponse This response contains the added task
	 */
	public function addTask($name, $user)
	{
		Core\CoreCommons\Logger::Info("addTask $name");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$task = Core\CoreContracts\Task::Create($name, $user);
			
			$result = $session->Save($task);
			
			$response = new Core\CoreCommons\ServiceResponse($result);
			
			$user = $session->Load('User', $user);
			
			if(isset($user) == false)
			{
				throw new \Exception("L'utilisateur #$user est introuvable");
			}
			
			$session->Close();
			Core\CoreCommons\Logger::Info("Task $name added");

			Core\CoreCommons\Logger::Info(sprintf("Send a mail to the user %s in order to inform him he have a new task", $user));
			$values = array();
			$values['#firstname'] = $user->Firstname;
			$values['#lastname'] = $user->Lastname;
			$mail = new Core\CoreCommons\Mail("NewTask", $values); 
			$sent = $mail->Send($user->Email);
			
			if($sent == false)
			{
				throw new \Exception("Le mail n'a pas pu être transmis...");
			}
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	 * Get user's tasks
	 * 
	 * @param int $user The user id
	 *
	 * @return ServiceResponse This response contains all tasks
	 */
    public function getTasks($user)
	{
		Core\CoreCommons\Logger::Info("Service - getTasks");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
		
			$session->CreateCriteria('Task');
			$session->AddCriteria(Core\Datastorage\Expression::SQL_EQ('AffectedUser', $user));
			$result = $session->ListResult();
			
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			
			Core\CoreCommons\Logger::Info("Service - getTasks completed");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	 * Get all tasks
	 *
	 * @return ServiceResponse This response contains all tasks
	 */
    public function getAllTasks()
	{
		Core\CoreCommons\Logger::Info("Service - getAllTasks");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
		
			$session->CreateCriteria('Task');
			$result = $session->ListResult();
			
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			
			Core\CoreCommons\Logger::Info("Service - getAllTasks completed");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	 * Delete a task
	 * 
	 * @param integer $draining The unique task identifier
	 *
	 * @return ServiceResponse This response contains the deleted task
	 */
	public function deleteTask($id)
	{
		Core\CoreCommons\Logger::Info("deleteTask $id");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$task = $session->Load('Task', $id);

			$result = $session->Delete($task);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Task $id deleted");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	 * Update a task
	 * 
	 * @param integer $id The task id 
	 * @param integer $status The new task status 
	 *
	 * @return ServiceResponse This response contains the updated task
	 */
	public function updateTask($id, $status)
	{
		Core\CoreCommons\Logger::Info("updateTask $id, $status");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$task = $session->Load('Task', $id);
			
			if(isset($task) == false)
			{
				throw new \Exception("La tache #$id est introuvable");
			}
			
			$task->State = $status;
			$task->ModificationDate = date("Y-m-d H:i:s");
			
			$fieldsToUpdate = array('State' => $task->State, 'ModificationDate' => $task->ModificationDate);

			$result = $session->Update($task, $fieldsToUpdate);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Task $id updated");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
}

?> 